package com.tencent.wxcloudrun.response;

import com.tencent.wxcloudrun.utils.ThreadLocalTool;

/**
 * ClassName: R
 * Package: com.tencent.wxcloudrun.response
 * Description:
 *
 * @Date: 2023/1/22 9:01 AM
 * @Author: wht
 */
public class R<T> {
    //服务端返回的错误码
    private int code;
    //服务端返回的错误信息
    private String msg;
    //我们服务端返回的数据
    private T data;

    private String token;


    private R(int code, String msg, T data) {
        this.code = code;
        if (msg != null) {
            this.msg = msg;
        }
        if (data != null) {
            this.data = data;
        }
        this.token = ThreadLocalTool.getToken();
    }

    public static <T> R successMsg(String msg) {
        R res = new R(200, msg, null);
        return res;
    }

    public static <T> R successData(T data) {
        R res = new R(200, null, data);
        return res;
    }

    public static <T> R successMsgData(String msg, T data) {
        R res = new R(200, msg, data);
        return res;
    }

    public static <T> R error(ErrorCodeMsgEnum errorCodeMsgEnum) {
        R res = new R(errorCodeMsgEnum.getCode(), errorCodeMsgEnum.getMsg(), null);
        return res;
    }


    public int getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public String getToken() {
        return this.token;
    }

    public T getData() {
        return this.data;
    }


    // 添加 -----------------------
    public static <T> R AddSuccess() {
        return successMsg("添加成功");
    }

    public static <T> R AddFail() {
        R res = new R(201, "添加失败", null);
        return res;
    }

    // 修改 -----------------------
    public static <T> R UpdateSuccess() {
        return successMsg("修改成功");
    }

    public static <T> R UpdateFail() {
        R res = new R(201, "修改失败", null);
        return res;
    }

    // 删除 -----------------------
    public static <T> R DelSuccess() {
        return successMsg("删除成功");
    }

    public static <T> R DelFail() {
        R res = new R(201, "删除失败", null);
        return res;
    }


}
